<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=1000, initial-scale=1.0, maximum-scale=1.0">
	    <!-- Loading Bootstrap -->
	    <link href="./Public/Admin/Flat/dist/css/vendor/bootstrap.min.css" rel="stylesheet">
	    <!-- Loading Flat UI -->
	    <link href="./Public/Admin/Flat/dist/css/flat-ui.css" rel="stylesheet">
	    <link rel="shortcut icon" href="./Public/Admin/Flat/img/favicon.ico">
		<script type="text/javascript" src="Public/uploadify/jquery-1.8.2.min.js"></script>
		<script type="text/javascript" src="Public/uploadify/jquery.uploadify.min.js"></script>
		<link rel="stylesheet" type="text/css" href="Public/uploadify/uploadify.css">
		<!--		载入百度编辑器-->
		<script type="text/javascript" charset="utf-8" src="Public/Admin/ueditor1_4_3/ueditor.config.js"></script>
		<script type="text/javascript" charset="utf-8" src="Public/Admin/ueditor1_4_3/ueditor.all.min.js"> </script>
		<script type="text/javascript" charset="utf-8" src="Public/Admin/ueditor1_4_3/lang/zh-cn/zh-cn.js"></script>
	    <!-- HTML5 shim, for IE6-8 support of HTML5 elements. All other JS at the end of file. -->
	    <!--[if lt IE 9]>
	      <script src="dist/js/vendor/html5shiv.js"></script>
	      <script src="dist/js/vendor/respond.min.js"></script>
	    <![endif]-->
	</head>
	<body>
<div class="span12">
	<form action="" method='post'  enctype="multipart/form-data">
		<fieldset>
			<legend>添加商品</legend>
			<span class="btn btn-primary">基本信息</span>
			<table class='table table-bordered table-hover'>
				<tbody>
				<tr class="info">
					<td>所属分类</td>
					<td>
						<select name="shop_category_cid" class="sel">
							<option value="0">-请选择-</option>
							<?php foreach ($cateData as $u){?>
							<option value="<?php echo $u['cid']?>" tid=<?php echo $u['shop_type_tid']?> class="opt"><?php echo $u['_name']?></option>
							<?php }?>
						</select>
						<script>
							$(function(){
								$('.sel').change(function(){
//									获取selected里的tid的值，放在隐藏域中，再追加到form中
									var tid=$('option:selected').attr('tid');
									var i='<input type="hidden" name="shop_type_tid" value="'+tid+'"/>';
									$("form").append(i);
//									打印
									console.log(tid);
									//发送异步
									$.ajax({
										type:"post",
										url:"<?php echo U('Commod/add')?>",
										data:{t:tid},
										//指定服务器返回的数据类型
										dataType:'json',
										success:function(phpData){
											console.log(phpData);
											var  attr='';
											var  spec='';
											$.each(phpData,function(k,v){
//												判断是规格、还是属性
												if(v.tatype==1){
												    attr+='<tr class="info">';
													attr+='<td>'+v.taname+'</td>';
													attr+='<td>';
													attr+='<select name="attr['+ v.taid+']">';
													attr+='<option value="0">-请选择-</option>';
													$.each(v.tavalue, function(kk,vv) {
														attr+='<option value="'+vv+'">'+vv+'</option>';
													});
													attr+='</select>';
													attr+='</td>';
													attr+='</tr>';
												}else{
//													添加规格
													spec+='<tr class="info">';
													spec+='<td>'+v.taname+'</td>';
													spec+='<td>';
													spec+='<select name="spec[value]['+v.taid+'][]">';
													spec+='<option value="0">-请选择-</option>';
													$.each(v.tavalue, function(kk,vv) {
														spec+='<option value="'+vv+'">'+vv+'</option>';
													});
													spec+='</select>';
													spec+='</td>';
													spec+='<td>附加价格</td>';
													spec+='<td>';
													spec+='<input type="text" name="spec[added]['+v.taid+'][]"/>';
													spec+='</td>';
													spec+='<td><a href="javascript:;" class="btn btn-primary addSpec">添加规格</a></td>';
													spec+='</tr>';
												}
											});
											$('#attr').html(attr);
											$('#spec').html(spec);
										}
									});
								})
								//点击添加规格
								$('.addSpec').live('click',function(){
									//找到当前的父级的tr
									var trObj = $(this).parents('tr');
									//克隆一个
									var tr = trObj.clone();
									tr.find('.addSpec').html('删除规格').removeClass('btn-primary addSpec').addClass('btn-info removeSpec');
									//再插入到当前父级的下面
									trObj.after(tr);

								})
								$('.removeSpec').live('click',function(){
									$(this).parents('tr').remove();
								})
							})
						</script>
					</td>
				</tr>
				<tr class="info">
					<td>所属品牌</td>
					<td>
						<select name="shop_brand_bid">
							<option value="0">-请选择-</option>
							<?php foreach ($brandData as $u){?>
							<option value="<?php echo $u['bid']?>"><?php echo $u['bname']?></option>
							<?php }?>
						</select>
					</td>
				</tr>
				<tr class="info">
					<td>商品名称</td>
					<td>
						<input type="text" name='coname'/>
					</td>
				</tr>
				<tr class="info">
					<td>商品编号</td>
					<td>
						<input type="text" name='conum'/>
					</td>
				</tr>
<!--				<tr class="info">-->
<!--					<td>上架时间</td>-->
<!--					<td>-->
<!--						<input type="text" name='cotime'/>-->
<!--					</td>-->
<!--				</tr>-->
				<tr class="info">
					<td>单位</td>
					<td>
						<input type="text" name='counit' value='件'/>
					</td>
				</tr>
				<tr class="info">
					<td>市价场</td>
					<td>
						<input type="text" name='coshoppr'/>
					</td>
				</tr>
				<tr class="info">
					<td>商城价</td>
					<td>
						<input type="text" name='comallpr'/>
					</td>
				</tr>
				<tr class="info">
					<td>点击次数</td>
					<td>
						<input type="text" name='coclick'/>
					</td>
				</tr>
				<tr class="info">
					<td>库存</td>
					<td>
						<input type="text" name='costock'/>
					</td>
				</tr>
				</tbody>
<!--				隐藏域-->
<!--				管理员ID-->
				<input type="hidden" name="shop_admin_aid" value="<?php echo $_SESSION['info']['aid']?>">
			</table>
			<p class="btn btn-primary">商品属性</p>
			<table class="table table-bordered table-hover" id='attr' class="info">

			</table>
			<p class="btn btn-primary">商品规格</p>
			<table class="table table-bordered table-hover" id='spec'>

			</table>
			<span  class="btn btn-primary">列表图</span>
			<table class='table table-bordered'>
				<tr class="info">
					<td>上传图片</td>
					<td>
						<div lab="uploadFile">
							<!-- file表单 -->
							<input id="f" type="file" multiple="true">
							<span>类型: *.jpg,*.png 大小: 2000KB 数量: 10</span>
							<script type="text/javascript">
								$(function() {
									$('#f').uploadify({
										'formData'     : {//POST数据
											'<?php echo session_name();?>' : '<?php echo session_id();?>',
										},
										'fileTypeExts' : '*.jpg;*.png',
										'swf'      : '<?php echo __PUBLIC__?>/uploadify/uploadify.swf',
										'uploader' : '<?php echo U('upload')?>',//指定服务器上传的方法
										'buttonText':'选择文件',
										'fileSizeLimit' : '2000KB',
										'uploadLimit' : 1,//上传文件数
										'width':65,
										'height':25,
										'successTimeout':10,//等待服务器响应时间
										'removeTimeout' : 0.2,//成功显示时间
										'onUploadSuccess' : function(file, data, response) {
											//把php返回的数据转为json
											data=$.parseJSON(data);
											//获得图片地址
											var imageUrl = data.url;
											var li="<li>";
											li += "<img src='"+imageUrl+"'/>";
											li += "<input type='hidden' id='inputimg' name='colistimg' value='"+data.path+"'/><a href='javascript:;' id='self-close0'>X</a>";
											li += "</li>";
											$("#uploadListd ul").prepend(li);
										}
									});

									//关闭图片
									var i = 1;
									$('#self-close0').live('click',function(){
										var src=$('#inputimg').attr('value');
										$.ajax({
											type:"post",
											url:"<?php echo U('Commod/deleteimg')?>",
											data:{path:src}
										})
										$(this).parent('li').remove();
										i++;
										$('#f').uploadify('settings','uploadLimit',i);
									})
								});
							</script>
							<div id="uploadListd">
								<ul>

								</ul>
							</div>
						</div>
					</td>
					<td id='pic-list'></td>
				</tr>

			</table>
			<span  class="btn btn-primary">商品图册</span>
			<table class='table table-bordered'>
				<tr class="info">
					<td>上传图片</td>
					<td>
						<div lab="uploadFile">
							<!-- file表单 -->
							<input id="commodf" type="file" multiple="true">
							<span>类型: *.jpg,*.png 大小: 2000KB 数量: 5</span>
							<script type="text/javascript">
								$(function() {
									var index=0;
									$('#commodf').uploadify({
										'formData'     : {//POST数据
											'<?php echo session_name();?>' : '<?php echo session_id();?>',
										},
										'fileTypeExts' : '*.jpg;*.png',
										'swf'      : '<?php echo __PUBLIC__?>/uploadify/uploadify.swf',
										'uploader' : '<?php echo U('upload')?>',//指定服务器上传的方法
										'buttonText':'选择文件',
										'fileSizeLimit' : '2000KB',
										'uploadLimit' : 5,//上传文件数
										'width':65,
										'height':25,
										'successTimeout':10,//等待服务器响应时间
										'removeTimeout' : 0.2,//成功显示时间
										'onUploadSuccess' : function(file, data, response) {
											//把php返回的数据转为json
											data=$.parseJSON(data);
											//获得图片地址
											var imageUrl = data.url;
											if(index>=5){index=0;}
											var li="<li>";
											li += "<img src='"+imageUrl+"'/>";
											li += "<input type='hidden' id='inputimg"+index+"' name='coimg[]' value='"+data.path+"'/><a href='javascript:;' id='self-close"+index+"'>X</a>";
											li += "</li>";
											$("#uploadList ul").prepend(li);
											index++;
										}
									});

									//关闭图片
									var i = 1;
									<?php $num=['0'=>0,'1'=>1,'2'=>2,'3'=>3,'4'=>4,]?>
									<?php foreach ($num as $v){?>
									$('#self-close<?php echo $v?>').live('click',function(){
										var src=$('#inputimg<?php echo $v?>').attr('value');
										$.ajax({
											type:"post",
											url:"<?php echo U('Commod/deleteimg')?>",
											data:{path:src}
										})
										$(this).parent('li').remove();
										i++;
										$('#commodf').uploadify('settings','uploadLimit',i);
									})
									<?php }?>
								});
							</script>
							<div id="uploadList">
								<ul>

								</ul>
							</div>
						</div>
					</td>
					<td id='photo-list'></td>
				</tr>
			</table>
			<table class='table'>
				<tr class="next_show">
					<th class="btn btn-primary">商品详细</th>
				</tr>
				<tr class="info">
						<!--			调用百度编辑器-->
						<script id="editor" type="text/plain" style="width:100%;height:500px;" name="codetail"></script>
						<script>
							var ue = UE.getEditor('editor');
						</script>
<!--						<textarea name="intro" id="intro"></textarea>-->
				</tr>
				<script>
//					$(function(){
//						$('.btn').click(function(){
//
//						})
//					})
				</script>
			</table>
			<input type="hidden" name='shop_type_tid' value=''/>
			<input type="submit" value="确认添加" class="btn btn-primary btn-block btn-large" />
		</fieldset>
	</form>
</div>

	</body>
</html>
